package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectTech;
import com.abvnet.project.water.project.service.IProjectTechService;
import com.abvnet.project.water.sewage.service.ISewageFlowService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/project/projectTech")
@Slf4j
public class ProjectTechController extends BaseController {

    private String prefix = "project/projectInfo";

    @Autowired
    private IProjectTechService projectTechService;

    @Autowired
    private ISewageFlowService sewageFlowService;

    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        mmap.put("sfList", sewageFlowService.selectSewageFlowListByProjectId(projectId));
        mmap.put("projectTech", projectTechService.selectProjectTechByProjectId(projectId));
        return prefix + "/tech";
    }

    @Log(title = "工艺运行情况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectTech projectTech, @RequestParam("refluxFile") MultipartFile refluxFile,
                               @RequestParam("streamFile") MultipartFile streamFile,
                               @RequestParam("aerationFile") MultipartFile aerationFile,
                               @RequestParam("diveFile") MultipartFile diveFile,
                               @RequestParam("otherFile") MultipartFile otherFile) {
        try {
            if (refluxFile != null && !refluxFile.isEmpty()) {
                String refluxUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), refluxFile);
                projectTech.setRefluxPicUrl(refluxUrl);
            }
            if (streamFile != null && !streamFile.isEmpty()) {
                String streamUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), streamFile);
                projectTech.setStreamPicUrl(streamUrl);
            }
            if (aerationFile != null && !aerationFile.isEmpty()) {
                String aerationUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), aerationFile);
                projectTech.setAerationPicUrl(aerationUrl);
            }
            if (diveFile != null && !diveFile.isEmpty()) {
                String diveUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), diveFile);
                projectTech.setDivePicUrl(diveUrl);
            }
            if (otherFile != null && !otherFile.isEmpty()) {
                String otherUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), otherFile);
                projectTech.setOtherPicUrl(otherUrl);
            }
            return toAjax(projectTechService.updateProjectTech(projectTech));
        } catch (Exception e) {
            log.error("更新工艺运行情况失败！", e);
            return error(e.getMessage());
        }
    }
}
